package com.cqjtu.store.order.service.impl;

import com.cqjtu.store.common.*;
import com.cqjtu.store.order.mapper.OrderItemMapper;
import com.cqjtu.store.order.mapper.OrderMapper;
import com.cqjtu.store.order.pojo.dto.OrderListDTO;
import com.cqjtu.store.order.pojo.dto.OrderSearchDTO;
import com.cqjtu.store.order.pojo.entity.Order;
import com.cqjtu.store.order.pojo.entity.OrderItem;
import com.cqjtu.store.order.pojo.vo.OrderItemVO;
import com.cqjtu.store.order.pojo.vo.OrderVO;
import com.cqjtu.store.order.service.OrderService;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: cqjtu-store
 * @Titile: OrderServiceImpl
 * @Author: Lucky
 * @Description: 订单业务类
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;


    /**
     * 查询所有订单
     * @param userLoginVO
     * @return
     */
    @Override
    public List<OrderVO> getList(UserLoginVO userLoginVO, Integer status) {
        List<OrderVO> orderList = orderMapper.getOrderList(userLoginVO.getId(), status);
        if(orderList == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"此用户没有订单数据");
        }
        return orderList;
    }

    /**
     * 查询所有订单 cyz
     * @param userLoginVO
     * @return
     */
    @Override
    public List<OrderVO> getList(UserLoginVO userLoginVO, OrderListDTO orderListDTO) {
        System.out.println(orderListDTO);
        List<OrderVO> orderList = orderMapper.getOrderList1(userLoginVO.getId(),orderListDTO.getOrderId(),orderListDTO.getStatus());
        if(orderList == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"此用户没有订单数据");
        }
        return orderList;
    }

    /**
     * 确认收货
     * @param userLoginVO
     * @param oid
     */
    @Override
    public void receive(UserLoginVO userLoginVO, Integer oid) {
        Order orderById = this.orderMapper.getOrderById(oid);
        if (orderById == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "订单不存在");
        }
        if (orderById.getStatus() == 3){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "订单已经确认收货");
        }
        int receive = this.orderMapper.receive(oid, Constants.ALREADY_RECEIVE);
        if (receive != 1){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED, "修改失败！");
        }
    }

    @Override
    public PageVO<List<OrderVO>> getAllList(PageDTO pageDTO) {
        List<Order> orderList = orderMapper.getAllOrderListByPage(pageDTO);
        System.out.println(orderList);
        List<OrderVO> orderVOList = new ArrayList<>();
        orderList.forEach(order -> {
            OrderVO orderVO = new OrderVO();
            orderVO.setId(order.getId());
            orderVO.setRecvName(order.getRecvName());
            orderVO.setRecvAddress(order.getRecvAddress());
            orderVO.setRecvPhone(order.getRecvPhone());
            orderVO.setStatus(order.getStatus());
            orderVO.setIsDelete(order.getIsDelete());
            orderVO.setOrderTime(order.getOrderTime());
            orderVO.setTotalPrice(order.getTotalPrice());
            List<OrderItem> orderItemList=orderItemMapper.getById(order.getId());
            orderVO.setOrderItemVOList(getListOrderItem(orderItemList));
            orderVOList.add(orderVO);
        });
        if(orderList == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"此用户没有订单数据");
        }
        int count = orderMapper.count();
        PageVO<List<OrderVO>> pageVO =new PageVO<>(pageDTO.getPageIndex(), pageDTO.getPageSize(), count,orderVOList);
        return pageVO;
    }

    @Override
    public Integer getTotal() {
        Integer total = orderMapper.getTotal();
        return total;
    }

    @Override
    public void delete(Integer id) {
        int result = orderMapper.updateIsDelete(id);
        if(result != 1){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"删除失败");
        }
    }

    @Override
    public PageVO<List<OrderVO>> getSomeList(OrderSearchDTO orderSearchDTO) {
        List<Order> orderList = orderMapper.getSomeOrderListByPage(orderSearchDTO);
        System.out.println(orderList);
        List<OrderVO> orderVOList = new ArrayList<>();
        orderList.forEach(order -> {
            OrderVO orderVO = new OrderVO();
            orderVO.setId(order.getId());
            orderVO.setRecvName(order.getRecvName());
            orderVO.setRecvAddress(order.getRecvAddress());
            orderVO.setRecvPhone(order.getRecvPhone());
            orderVO.setStatus(order.getStatus());
            orderVO.setIsDelete(order.getIsDelete());
            orderVO.setOrderTime(order.getOrderTime());
            orderVO.setTotalPrice(order.getTotalPrice());
            List<OrderItem> orderItemList=orderItemMapper.getById(order.getId());
            orderVO.setOrderItemVOList(getListOrderItem(orderItemList));
            orderVOList.add(orderVO);
        });
        if(orderList == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"此用户没有订单数据");
        }
        int count = orderMapper.count();
        PageVO<List<OrderVO>> pageVO =new PageVO<>(orderSearchDTO.getPageIndex(), orderSearchDTO.getPageSize(), count,orderVOList);
        return pageVO;
    }

    public List<OrderItemVO> getListOrderItem(List<OrderItem> orderItemList) {
        List<OrderItemVO> orderItemVOList = new ArrayList<>();
        orderItemList.forEach(orderItem -> {
            OrderItemVO orderItemVO= new OrderItemVO();
            orderItemVO.setOrderId(orderItem.getOrderId());
            orderItemVO.setId(orderItem.getId());
            orderItemVO.setPrice(orderItem.getPrice());
            orderItemVO.setTitle(orderItem.getTitle());
            orderItemVO.setImage(orderItem.getImage());
            orderItemVO.setNum(orderItem.getNum());
            orderItemVO.setProductId(orderItem.getProductId());
            orderItemVOList.add(orderItemVO);
        });
        return orderItemVOList;
    }
}
